package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqCompostProduceInspectionMapper;
import com.guofen.pojo.SjqCompostProduceFault;
import com.guofen.pojo.SjqCompostProduceInspection;
import com.guofen.service.sjq.SjqCompostProduceInspectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqCompostProduceInspectionServiceImpl implements SjqCompostProduceInspectionService {
    @Autowired
    SjqCompostProduceInspectionMapper sjqCompostProduceInspectionMapper;

    @Override
    public boolean add(String equipment_name,String is_normal,String remarks,String operator){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);

        SjqCompostProduceInspection sjqCompostProduceInspection = new SjqCompostProduceInspection();
        sjqCompostProduceInspection.setDate(date);
        sjqCompostProduceInspection.setEquipmentName(equipment_name);
        sjqCompostProduceInspection.setIsNormal(is_normal);
        sjqCompostProduceInspection.setRemarks(remarks);
        sjqCompostProduceInspection.setOperator(operator);

        SjqCompostProduceInspection temp = sjqCompostProduceInspectionMapper.getlastByName(equipment_name);
        System.out.println(temp);
        if(temp != null){
            sjqCompostProduceInspection.setId(temp.getId());
            sjqCompostProduceInspection.setIsForm(null);
            sjqCompostProduceInspection.setInspector(null);
            int result = sjqCompostProduceInspectionMapper.updateByPrimaryKey(sjqCompostProduceInspection);
            if (result == 1)
                return true;
            else
                return false;
        }else{
            int result = sjqCompostProduceInspectionMapper.insert(sjqCompostProduceInspection);
            if (result == 1)
                return true;
            else
                return false;
        }
    }

    @Override
    public boolean update(String id,String inspector,String remarks){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);

        SjqCompostProduceInspection sjqCompostProduceInspection = new SjqCompostProduceInspection();
        sjqCompostProduceInspection.setId(Integer.parseInt(id));
        //sjqCompostProduceInspection.setDate(date);//设置最新还是安旧的来
        sjqCompostProduceInspection.setInspector(inspector);
        sjqCompostProduceInspection.setRemarks(remarks);

        int result = sjqCompostProduceInspectionMapper.updateByPrimaryKeySelective(sjqCompostProduceInspection);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public boolean set_is_from(String id){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);

        SjqCompostProduceInspection sjqCompostProduceInspection = new SjqCompostProduceInspection();
        sjqCompostProduceInspection.setId(Integer.parseInt(id));
        sjqCompostProduceInspection.setDate(date);//设置最新还是安旧的来
        sjqCompostProduceInspection.setIsForm(true);

        int result = sjqCompostProduceInspectionMapper.updateByPrimaryKeySelective(sjqCompostProduceInspection);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public List<SjqCompostProduceInspection> getlast_day(){
        return sjqCompostProduceInspectionMapper.getlast();
    }

    @Override
    public List<SjqCompostProduceInspection> getlastWithFault(){
        return sjqCompostProduceInspectionMapper.getlastWithFault();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqCompostProduceInspection> list = sjqCompostProduceInspectionMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqCompostProduceInspection> list = sjqCompostProduceInspectionMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqCompostProduceInspectionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqCompostProduceInspection record) {
        return sjqCompostProduceInspectionMapper.updateByPrimaryKeySelective(record);
    }

}
